package com.landun.market.web.interceptor;

import com.landun.market.service.ISecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author jiang.chuan
 * @date 2018-05-12
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
    private final ISecurityService securityService;

    @Autowired
    public InterceptorConfig(ISecurityService securityService) {
        this.securityService = securityService;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("*")
                .allowedOrigins("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthenticationInterceptor(securityService))
                .addPathPatterns("/**")
                .excludePathPatterns("/oauth2/**", "");
        super.addInterceptors(registry);
    }
}
